﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Confucius.entity;
using System.Windows.Resources;
using System.Xml.Linq;
using System.Linq;
using Confucius.utils;
using DBContent.Model;
using System.Collections.Generic;
using Confucius.dao;

namespace Confucius.daoImpl
{
    /// <summary>
    /// Author:Liangchuan Gu
    /// date:2012-04-08
    /// </summary>
    public class ItemDAOImpl : ItemDAO
    {
        public ItemDAOImpl(){}

        public Item readItem(String itemID, String gameID) 
        {
            Item item = new Item();

            try
            {
                Uri uri = new Uri("assets/gameFiles/" + gameID + "/item.xml", UriKind.Relative);
                StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
                XDocument loadedData = XDocument.Load(resourceInfo.Stream);

                var items = from record in loadedData.Descendants("item")
                               select record;

                foreach (XElement itemRecord in items)
                {
                    string id = (string)itemRecord.Element("itemID").Value;
                    if (id.Equals(itemID))
                    {
                        item.ItemID = id;
                        item.IconID = (string)itemRecord.Element("iconID").Value;
                        item.Lat = (float)Convert.ToDouble( (string)itemRecord.Element("lat").Value);
                        item.Lng = (float)Convert.ToDouble((string)itemRecord.Element("lng").Value);
                        item.Title = (string)itemRecord.Element("title").Value;
                        item.DisplayText = (string)itemRecord.Element("displayText").Value;
                        item.Pickable = (string)itemRecord.Element("pickable").Value;
                        item.ContainedMediaID = (string)itemRecord.Element("containedMediaID").Value;
                        // Get status from DB
                        DBItem dbItem = new DBItem();
                        dbItem.ItemID = id;
                        String itemStatus = ConfuciusDBHelper.getItemStatus(dbItem);

                        item.Status = itemStatus;
                        break;
                    }
                }

            }
            catch (Exception e)
            {
                throw new Exception("Could not read item from DB. Info is: " + e.Message);
            }

            return item;
        }

        public List<Item> retreiveItems(String gameID)
        {
            List<Item> items = new List<Item>();
            try
            {
                Uri uri = new Uri("assets/gameFiles/" + gameID + "/item.xml", UriKind.Relative);
                StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
                XDocument loadedData = XDocument.Load(resourceInfo.Stream);

                var itemRecords = from record in loadedData.Descendants("item")
                               select record;

                foreach (XElement record in itemRecords)
                {
                    Item newItem = new Item();

                    newItem.ItemID = (string)record.Element("itemID").Value;
                    newItem.Title = (string)record.Element("title").Value;
                    newItem.DisplayText = (string)record.Element("displayText").Value;
                    newItem.IconID = (string)record.Element("iconID").Value;
                    newItem.Lng = (float)Convert.ToDouble((string)record.Element("lng").Value);
                    newItem.Lat = (float)Convert.ToDouble((string)record.Element("lat").Value);
                    newItem.Pickable = (string)record.Element("pickable").Value;
                    newItem.ContainedMediaID = (string)record.Element("containedMediaID").Value;
                    // Get status from DB
                    DBItem dbItem = new DBItem();
                    dbItem.ItemID = newItem.ItemID;
                    String itemStatus = ConfuciusDBHelper.getItemStatus(dbItem);
                    newItem.Status = itemStatus;

                    items.Add(newItem);
                }
            }
            catch (Exception e)
            {
                throw new Exception("Could not load triggerIDs from DB. Info is: " + e.Message);
            }

            return items;
        }

        public void updateItemStatus(String itemID, String status)
        {
            DBItem dbItem = new DBItem();
            dbItem.ItemID = itemID;
            dbItem.Status = status;
            ConfuciusDBHelper.updateItemStatus(dbItem);
        }

        public String readItemStatus(String itemID) 
        {
            DBItem dbItem = new DBItem();
            dbItem.ItemID = itemID;
            String itemStatus = ConfuciusDBHelper.getItemStatus(dbItem);

            return itemStatus;
        }
    }
}
